package com.xxyuan.batch._02_params;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.Map;

@SpringBootApplication
@EnableBatchProcessing
public class ParamJob {
    /**
     * job调度器
     */
    @Autowired
    private JobLauncher jobLauncher;

    /***
     * job构造器工厂
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    /**
     * step构造器工厂
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    /**
     * 构造一个step对象执行的任务(逻辑对象)
     */
    @StepScope
    @Bean
    public Tasklet tasklet(@Value("#{jobParameters['name']}") String name) {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                //方案一 使用ChunkContext对象
                //Map<String, Object> jobParameters = chunkContext.getStepContext().getJobParameters();
                //System.out.println("param-name:"+jobParameters.get("name"));

                //方案二 使用@StepScope @Value延时获取Tasklet对象，在被调用才加载
                //要执行的逻辑-step1步骤执行逻辑
                System.out.println("================================================================");
                System.out.println("param-name:"+name);
                System.out.println("================================================================");
                return RepeatStatus.FINISHED;
            }
        };
    }

    /**
     * 构造一个step对象
     */
    @Bean
    public Step step1() {
        //tasklet step 执行逻辑，类似 Thread()--->可以执行runable接口
        return stepBuilderFactory.get("step1").tasklet(tasklet(null))
                .build();
    }

    /**
     * 构造一个job对象
     */
//    @Bean
//    public Job job() {
//        return jobBuilderFactory.get("param-job").start(step1()).build();
//    }

//

    @Bean
    public Job job() {
        return jobBuilderFactory.get("param-value-job").start(step1()).build();
    }

    public static void main(String[] args) {
        /**
         * Job Instance  =  Job名称  +  识别参数
         *
         * Job Instance 一次执行创建一个 Job Execution对象
         *
         * 完整的一次Job Instance 执行可能创建一个Job Execution对象，也可能创建多个Job  Execution对象
         */
         SpringApplication.run(ParamJob.class,args);
    }
}